package com.ruoyi.common.customize.util.login;

import com.ruoyi.common.base.user.vo.UserToken;
import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * token工具类
 *
 * @author gdk
 * @version 1.0.0
 * @since 2021/12/27 2:45 下午
 */
public class TokenUtil {

    /**
     * 根据用户创建loginUser对象
     *
     * @param token token
     * @return {@link LoginUser}
     * @author gdk
     * @since 2021/12/24
     */
    public static LoginUser createLoginUser(UserToken token) {
        LoginUser loginUser = new LoginUser(token);

        // 保存鉴权信息
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);

        return loginUser;
    }
}
